package com.darren.center.design.patterns.controller;

import com.darren.center.design.patterns.dto.Context;
import com.darren.center.design.patterns.utils.HtmlUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * <h3>design-patterns</h3>
 * <p></p>
 *
 * @author : Darren
 * @date : 2021年06月14日 19:29:13
 **/
public class BaseController {

    private static final Logger logger = LoggerFactory.getLogger(BaseController.class);
    public final static String CONTEXT = "COM.DARREN.CENTER_CONTEXT";

    public final static String CODE = "code";

    public final static String MSG = "msg";

    public final static String DATA = "data";

    public final static String REDIS_SESSION_USER = "redis_session_user";
    public final static String REDIS_SESSION_MERCHANT_USER = "redis_session_merchant_user";

    /**
     * 获取IP地址
     *
     * @param request
     * @return
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    /**
     * 所有ActionMap 统一从这里获取
     *
     * @return
     */
    public Map<String, Object> getRootMap() {
        Map<String, Object> rootMap = new HashMap<String, Object>();
        // 添加url到 Map中
        // rootMap.putAll(URLUtils.getUrlMap());
        return rootMap;
    }

    public ModelAndView forword(String viewName, Map<String, ?> context) {
        return new ModelAndView(viewName, context);
    }

    public ModelAndView error(String errMsg) {
        return new ModelAndView("error");
    }

    /**
     *
     * 提示成功信息
     *
     * @param message
     *
     */
    public void sendSuccessMessage(HttpServletResponse response, String message) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put(CODE, 0);
        result.put(MSG, message);
        HtmlUtil.writerJson(response, result);
    }

    public void sendSuccessData(HttpServletResponse response, Object data) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put(CODE, 0);
        result.put(DATA, data);
        HtmlUtil.writerJson(response, result);
    }

    public void sendCustomizeData(HttpServletResponse response, String data ,String contextType) {
        HtmlUtil.writerCustomize(response, data,contextType);
    }

    /**
     *
     * 提示失败信息
     *
     * @param message
     *
     */
    public void sendFailureMessage(HttpServletResponse response, int code, String message) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put(CODE, code);
        result.put(MSG, message);
        HtmlUtil.writerJson(response, result);
    }

    public static Context getContext(HttpServletRequest request) {
        return (Context) request.getAttribute(CONTEXT);
    }

}

